package com.example.everying.framework.websocket.handler;

import com.alibaba.fastjson2.JSONObject;
import com.example.everying.framework.websocket.service.WebSocketChatService;
import com.example.everying.service.chat.ChatConversationService;
import jakarta.websocket.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class PinnedMessageHandler implements MessageHandler{

    private static ChatConversationService chatConversationService;
    @Autowired
    private void setService(ChatConversationService chatConversationService){
        PinnedMessageHandler.chatConversationService = chatConversationService;
    }
    /**
     * @param session
     * @param message
     */
    @Override
    public void handle(Session session, JSONObject message) {
        String conversationId = message.getString("conversationId");
        boolean isPinned = message.getBoolean("isPinned");
        String userId = WebSocketChatService.getUserId(session);
      boolean res =  chatConversationService.pinConversation(conversationId, isPinned, userId);
      JSONObject jsonObject = new JSONObject();
      jsonObject.put("type", "pinned");
      jsonObject.put("conversationId", conversationId);
      jsonObject.put("isPinned", res);
      WebSocketChatService.sendMessageToUserId(userId, jsonObject.toJSONString());

    }
}
